<template>
  <div class="page-container">
    <div class="control-container">
      <div class="right-panel">
        <div class="search-panel">
          <el-form :inline="true" label-width="100">
            <el-form-item label="监测因子类型">
              <el-select v-model="query.factorType" class="w-100" placeholder="请选择类型">
                <el-option
                  v-for="(item,index) in factorTypes"
                  :key="index+'task_status'"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="监测因子名称">
              <el-input v-model="query.name" />
            </el-form-item>
            <el-form-item label="监测因子编码">
              <el-input v-model="query.num" />
            </el-form-item>
            <el-button type="primary" @click="handleSelect">查询</el-button>
            <el-button type="primary" @click="resetForm()">重置</el-button>
            <el-button v-if="hasBtn('library:add')" type="primary" @click="handleAddOrDetail('add')">添加</el-button>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 400px)"
            :data="tableData"
          >
            <el-table-column align="center" type="index" width="80" />
            <el-table-column prop="name" label="名称" class-name="color-green" />
            <el-table-column prop="num" label="HJT 212-2017" />
            <el-table-column prop="oldNum" label="HJT 212-2005" />
            <el-table-column prop="measurementUnit" :formatter="getUnit" label="计量单位" />
            <el-table-column prop="measurementUnitRate" :formatter="getRates" label="计量单位倍率" />
            <el-table-column prop="countUnit" :formatter="getUnit2" label="累计单位" />
            <el-table-column prop="countUnitRate" :formatter="getRates2" label="累计单位倍率" />
            <el-table-column prop="digit" :formatter="getDigits" label="小数点位数" />
            <el-table-column prop="lowerAlarmLimit" label="报警下限值" />
            <el-table-column prop="upperAlarmLimit" label="报警上限值" />
            <el-table-column prop="factorType" label="类型">
              <template slot-scope="scope">{{ getDictLabel('e012', scope.row.factorType) }}
              </template>
            </el-table-column>
            <el-table-column v-if="hasBtn('library:edit')" label="操作" width="150">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="handleAddOrDetail('update',scope.row)">修改</el-button>
                <el-button v-if="hasBtn('library:del')" type="primary" size="small" @click="handleDel(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddFactorLibraryDialog
      :dialog-visible="dialogVisible"
      :dialog-type="dialogType"
      :obj="obj"
      :units="units"
      :rates="rates"
      :digits="digits"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getDict } from '@/utils/dict'
import { getPage, del } from '@/api/system/factorLibrary'
import { hasBtnPermission as hasBtn } from '@/utils/buttons'
import { getDictLabel, parseTime } from '@/utils'
import AddFactorLibraryDialog from '@/views/basicInfo/components/AddFactorLibraryDialog'
import { getSectionList } from '@/api/info/allInfo'

export default {
  name: 'FactorLibrary',
  components: {
    AddFactorLibraryDialog,
    Pagination
  },
  data() {
    return {
      tableLoading: false,
      query: {
        page: 1, limit: 10, total: 0,
        factorType: null, name: null, num: null
      },
      tableData: [],
      dialogVisible: false,
      dialogType: 'detail',
      factorTypes: [],
      obj: {},
      previewDialogVisible: false,
      units: [],
      rates: [],
      digits: []
    }
  },
  created() {
    this.factorTypes = getDict('e012')
    this.getAllInfo()
    this.initData()
  },
  methods: {
    getDictLabel, parseTime, hasBtn,
    initData() {
      const query = JSON.parse(JSON.stringify(this.query))
      this.tableLoading = true
      getPage(query).then(result => {
        this.tableLoading = false
        this.query.total = result.data.total
        this.tableData = result.data.list
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleDel(row) {
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del([row.id]).then(result => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.handleSelect()
        }).catch(() => {
        })
      }).catch(() => {
      })
    },
    resetForm() {
      this.query = {
        ...this.query,
        factorType: null, name: null, num: null
      }
    },
    // 查询
    handleSelect() {
      // this.query.page = 1
      this.initData()
    },
    // 添加
    handleAddOrDetail(dialogType, row) {
      this.dialogVisible = true
      this.dialogType = dialogType
      this.obj = row
    },
    getAllInfo() {
      getSectionList(['e039_09', 'e039_10', 'e039_11']).then(res => {
        this.allInfo = res.data.list
        this.allInfo.forEach(item => {
          if (item.infoType === 'e039_09') {
            this.units.push(item)
          } else if (item.infoType === 'e039_10') {
            this.rates.push(item)
          } else if (item.infoType === 'e039_11') {
            this.digits.push(item)
          }
        })
      })
    },
    getUnit(row) {
      for (const i in this.units) {
        if (this.units[i].id === row.measurementUnit) {
          return this.units[i].name
        }
      }
    },
    getUnit2(row) {
      for (const i in this.units) {
        if (this.units[i].id === row.countUnit) {
          return this.units[i].name
        }
      }
    },
    getRates(row) {
      for (const i in this.rates) {
        if (this.rates[i].id === row.measurementUnitRate) {
          return this.rates[i].name
        }
      }
    },
    getRates2(row) {
      for (const i in this.rates) {
        if (this.rates[i].id === row.countUnitRate) {
          return this.rates[i].name
        }
      }
    },
    getDigits(row) {
      for (const i in this.digits) {
        if (this.digits[i].id === row.digit) {
          return this.digits[i].name
        }
      }
    },
    flushPage() {
      this.clearQuery()
      this.initData()
    },
    clearQuery() {
      this.query = {
        page: 1, limit: 10, total: 0,
        factorType: null, name: null, num: null
      }
    },
    closeDialog() {
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.download{
  margin: 0px 8px 4px 8px !important;
}
</style>
